package com.wl.cloud.system.controller;

import com.wl.cloud.core.dto.DataStoreDTO;
import com.wl.cloud.core.dto.RestResultDTO;
import com.wl.cloud.system.service.SysResourceCategoryService;
import com.wl.cloud.system.support.dto.SysResourceCategoryDTO;
import com.wl.cloud.system.support.dto.query.SysResourceCategoryQueryDTO;
import com.wl.cloud.system.support.utils.ConstantSystem;
import com.wl.cloud.system.support.vo.SysResourceCategoryVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.data.web.SortDefault;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * 后台资源类型表 前端控制器
 *
 * @author wanglin
 * @since 2023-09-18
 */
@Tag(name = "后台资源类型表", description = "SysResourceCategoryController")
@Validated
@CrossOrigin
@RestController
@RequestMapping(ConstantSystem.PROJECT_URL_PREFIX + "sys/resourceCategory")
public class SysResourceCategoryController {

    @Autowired
    private SysResourceCategoryService sysResourceCategoryService;

    /**
     * 分页查询
     *
     * @param pageable
     * @return
     */
    @Operation(summary = "分页")
    @RequestMapping(value = "/page", method = {RequestMethod.POST, RequestMethod.GET})
    public RestResultDTO<DataStoreDTO<SysResourceCategoryVO>> page(@ParameterObject @PageableDefault(sort = "createTime", direction = Sort.Direction.DESC) Pageable pageable,
                                                                   @ParameterObject SysResourceCategoryQueryDTO queryDto) {
        return RestResultDTO.newSuccess(sysResourceCategoryService.page(pageable, queryDto));
    }

    /**
     * 列表查询
     *
     * @param sort
     * @return
     */
    @Operation(summary = "列表")
    @RequestMapping(value = "/list", method = {RequestMethod.POST, RequestMethod.GET})
    public RestResultDTO<List<SysResourceCategoryVO>> list(@ParameterObject @SortDefault(sort = "createTime", direction = Sort.Direction.DESC) Sort sort,
                                                           @ParameterObject SysResourceCategoryQueryDTO queryDto) {
        return RestResultDTO.newSuccess(sysResourceCategoryService.list(sort, queryDto));
    }

    /**
     * 保存
     *
     * @param dto
     * @return
     */
    @Operation(summary = "保存")
    @PostMapping("/save")
    public RestResultDTO<Void> save(@Validated @RequestBody SysResourceCategoryDTO dto) {
        sysResourceCategoryService.save(dto);
        return RestResultDTO.newSuccess();
    }

    /**
     * 修改
     *
     * @param dto
     * @return
     */
    @Operation(summary = "修改")
    @PostMapping(value = "/update")
    public RestResultDTO<Void> update(@Validated @RequestBody SysResourceCategoryDTO dto) {
        sysResourceCategoryService.update(dto);
        return RestResultDTO.newSuccess();
    }

    /**
     * 查询详情
     *
     * @param id
     * @return
     */
    @Operation(summary = "查询详情")
    @GetMapping("/get")
    public RestResultDTO<SysResourceCategoryVO> get(@Parameter(description = "记录ID") @RequestParam String id) {
        return RestResultDTO.newSuccess(sysResourceCategoryService.get(id));
    }

    /**
     * 删除
     *
     * @param ids
     * @return
     */
    @RequestMapping(value = "/delete", method = {RequestMethod.GET})
    public RestResultDTO<Void> delete(@RequestParam Set<String> ids) {
        sysResourceCategoryService.delete(ids);
        return RestResultDTO.newSuccess(null, "删除成功");
    }

    @Operation(summary = "树形资源列表")
    @RequestMapping(value = "treeResourceList", method = {RequestMethod.POST, RequestMethod.GET})
    public RestResultDTO<List<SysResourceCategoryVO>> treeResourceList(@ParameterObject SysResourceCategoryQueryDTO queryDto) {
        return RestResultDTO.newSuccess(sysResourceCategoryService.treeResourceList(queryDto));
    }

    @Operation(summary = "树形资源列表")
    @RequestMapping(value = "treeResourcePage", method = {RequestMethod.POST, RequestMethod.GET})
    public RestResultDTO<DataStoreDTO<SysResourceCategoryVO>> treeResourcePage(@ParameterObject @PageableDefault(sort = "createTime", direction = Sort.Direction.DESC) Pageable pageable,
                                                                       @ParameterObject SysResourceCategoryQueryDTO queryDto) {
        return RestResultDTO.newSuccess(sysResourceCategoryService.treeResourcePage(pageable, queryDto));
    }
}
